package org.sparkle.db.company;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.sparkle.app.AbstractApplication;
import org.sparkle.app.BaseApplication;
import org.sparkle.db.basic.BaseVO;

import com.avaje.ebean.validation.NotNull;

@Entity
@Table(name = "spk_company")
public class CompanyVO extends BaseVO {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8737985714616579191L;
	@NotNull
	@Column(unique=true)
	private String name;
	@NotNull
	@Column(unique=true)
	private String hostname; 
	private String applicationClass;

	@Transient
	private AbstractApplication application;
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public String getHostname() {
		return hostname;
	}

	public void setHostname(String hostname) {
		this.hostname = hostname;
	}

	public String getApplicationClass() {
		return applicationClass;
	}

	public void setApplicationClass(String applicationClass) {
		this.applicationClass = applicationClass;
	}

	public AbstractApplication getApplication(){
		if(application==null){
			if(getApplicationClass()==null)
				application=new BaseApplication(getName());
			else
				try {
					application=(AbstractApplication) getClass().getClassLoader().loadClass(getApplicationClass()).newInstance();
				} catch (InstantiationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		return application;
	}

}
